﻿var grid;
var varIncrementer = 0;
var v_TrnInvoiceID = varIncrementer++,
        v_Sr = varIncrementer++,
        v_CompanyName = varIncrementer++,
        v_ClientName = varIncrementer++,
        v_ContractName = varIncrementer++,
        v_PaymentModeName = varIncrementer++,
        v_PaymentLevelName = varIncrementer++,
        v_ContractNo = varIncrementer++,
        v_InvoiceNo = varIncrementer++,
        v_InvoiceDate = varIncrementer++,
        v_PaymentLevelPer = varIncrementer++,
        v_GrossInvoiceAmount = varIncrementer++,
        v_ServiceTaxPer = varIncrementer++,
        v_ServiceTaxAmount = varIncrementer++,
        v_FinalInvoiceAmount = varIncrementer++;

$(document).ready(function () {
    $('#cboCompany').select2();
    $('#cboClient').select2();
    $('#cboContract').select2();
    $('#cboPaymentMode').select2(); 
     

    $('#cboCompany').focus();
});

function fn_GetSearchResult(p_vTrack, p_objResult) {
    if (p_vTrack == "0") {
        callBack('GetSearchResult', 'cboCompany@cboContract@cboClient@cboPaymentMode@txtContractNo@txtInvoiceNo@txtInvoiceStartDate@txtInvoiceEndDate');
    }
    else {
        document.getElementById("divResult").innerHTML = p_objResult.grdResult;
    }
    return false;
}

function ShowEditDiv(objID, objControl) {
    Page_ClientValidate('');

    //For Edit
    if (objID != 0) {
        var varRow = objControl.parentNode.parentNode;
        document.getElementById('hdnInvoiceID').value = objID;
        var vAdditionalTrack = "RowID@@" + varRow.cells[v_Sr].innerText;
        $('#divInsertEdit').dialog('open');
        $('#divInsertEdit').dialog("option", "title", 'Editing Invoice for [' + varRow.cells[v_InvoiceNo].innerText + ']');
        callBack('EdiRecord', 'hdnInvoiceID', vAdditionalTrack);

    }
    else {//For Insert
        document.getElementById('hdnInvoiceID').value = "0";
        $('#divInsertEdit').dialog('open');
        $('#hdnInvoiceID').val("");
        $("#cboAddCompany").select2("val", "0");
        $('#cboAddClient').select2("val", "0");
        $('#cboAddContract').select2("val", "0");
        $('#cboAddTrnContract').select2("val", "0");
        $('#txtAddInvoiceDate').val("");
        $('#txtGrossInvoiceAmt').val("");
        $('#txtAddContractPer').val("");
        $('#txtAddServiceTaxPer').val("");
        $('#hdnServiceTaxPer').val("");
        $('#txtServiceTaxAmt').val("");
        $('#txtFinalInvoiceAmt').val("");
        $('#hdnPaymentModeID').val("");
        $('#hdnPaymentLevelID').val("");
        $('#lblPaymentLevel').text("");
        $('#lblPaymentMode').text("");
        $('#lblContractAmount').text("");

        $('#cboAddClient').find("option:gt(0)").remove();
        $('#cboAddContract').find("option:gt(0)").remove();
        $('#cboAddTrnContract').find("option:gt(0)").remove();

        document.getElementById('cboAddCompany').disabled = false;
        document.getElementById('cboAddClient').disabled = false;
        document.getElementById('cboAddContract').disabled = false;
        document.getElementById('cboAddTrnContract').disabled = false;

        $('#divInsertEdit').dialog("option", "title", 'Add Invoice');
    }
}

function fn_EdiRecord_Res(objResult) {
    var varRow = document.getElementById('grdResult').rows[objResult.RowID];
    //$('#divInsertEdit').dialog('open');
    // $('#divInsertEdit').dialog("option", "title", 'Edit Invoice For [Invoice # ' + varRow.cells[v_ContractNo].innerText + ']');

    $('#cboCompanyAdd').find("option:gt(0)").remove();
    $('#cboAddClient').find("option:gt(0)").remove();
    $('#cboAddContract').find("option:gt(0)").remove();
    $('#cboAddTrnContract').find("option:gt(0)").remove();

    $('#txtAddContractPer').focus();
    document.getElementById("cboAddClient").innerHTML = objResult.cboAddClient;
    $('#cboAddClient').select2();
    document.getElementById("cboAddContract").innerHTML = objResult.cboAddContract;
    $('#cboAddContract').select2();
    document.getElementById("cboAddTrnContract").innerHTML = objResult.cboAddTrnContract;
    $('#cboAddTrnContract').select2();

    $('#cboAddCompany').select2("val", varRow.cells[v_CompanyID].innerText);
    $('#cboAddClient').select2("val", varRow.cells[v_ClientID].innerText);
    $('#cboAddContract').select2("val", varRow.cells[v_ContractID].innerText);
    $('#cboAddTrnContract').select2("val", varRow.cells[v_TrnContractID].innerText);
    $('#hdnPaymentLevelID').val(varRow.cells[v_PaymentLevelID].innerText);

    $('#lblPaymentMode').html(varRow.cells[v_PaymentModeName].innerText);
    $('#lblPaymentLevel').html(varRow.cells[v_PaymentLevelName].innerText);

    $('#txtAddInvoiceDate').val(varRow.cells[v_InvoiceDate].innerText);
    $('#txtAddContractPer').val(varRow.cells[v_PaymentLevelPer].innerText);
    $('#txtGrossInvoiceAmt').val(varRow.cells[v_GrossInvoiceAmount].innerText);
    $('#txtAddServiceTaxPer').val(varRow.cells[v_ServiceTaxPer].innerText);
    $('#hdnServiceTaxPer').val(varRow.cells[v_ServiceTaxPer].innerText);
    if ($('#txtAddServiceTaxPer').val() == "" || parseFloat($('#txtAddServiceTaxPer').val()) == 0)
        document.getElementById('chkIsServiceTax').checked = false;
    else
        document.getElementById('chkIsServiceTax').checked = true;

    $('#txtServiceTaxAmt').val(varRow.cells[v_ServiceTaxAmount].innerText);
    $('#txtFinalInvoiceAmt').val(varRow.cells[v_FinalInvoiceAmount].innerText);

    $('#lblContractAmount').html(objResult.lblContractAmount);

    document.getElementById('hdnCompletedContractPer').value = (parseFloat(objResult.hdnCompletedContractPer) - parseFloat(varRow.cells[v_PaymentLevelPer].innerText));
    document.getElementById('hdnMaxContractInvoice').value = objResult.hdnMaxContractInvoice;
    document.getElementById('hdnCompletedInvAmount').value = (parseFloat(objResult.hdnCompletedInvAmount) - parseFloat(varRow.cells[v_GrossInvoiceAmount].innerText));

    //document.getElementById('cboAddCompany').disabled = true;
    document.getElementById('cboAddClient').disabled = true;
    document.getElementById('cboAddContract').disabled = true;
    document.getElementById('cboAddTrnContract').disabled = true;

    if (objResult.IsLastPaymentMode == "True") {
        $("#txtAddContractPer").prop("readonly", true);
        $("#txtGrossInvoiceAmt").prop("readonly", true);
    }
    else {
        $("#txtAddContractPer").prop("readonly", false);
        $("#txtGrossInvoiceAmt").prop("readonly", false);
    }

}

function ShowDownloadPopup(objInvoiceID) {
    var vAdditionalTrack = "InvoiceID@@" + objInvoiceID;
    callBack('DownloadInvoice', '', vAdditionalTrack);
}

function fn_ClearAddControls() {

    $('#hdnPaymentLevelID').val("");

    $('#lblPaymentMode').html("");
    $('#lblPaymentLevel').html("");

    $('#txtAddInvoiceDate').val("");
    $('#txtAddContractPer').val("");
    $('#txtGrossInvoiceAmt').val("");
    $('#txtAddServiceTaxPer').val("");
    $('#txtServiceTaxAmt').val("");
    $('#txtFinalInvoiceAmt').val("");

    $('#lblContractAmount').html("");

    document.getElementById('hdnCompletedContractPer').value = ("");
    document.getElementById('hdnMaxContractInvoice').value = "";
    document.getElementById('hdnCompletedInvAmount').value = "";
}

function fn_SaveDetails() {

    if (Page_ClientValidate("vgSave")) {
        $('#btnSave').attr('disabled', true);
        vControlList = "cboAddTrnContract@cboAddCompany@txtAddInvoiceDate@hdnPaymentLevelID@txtAddContractPer@txtGrossInvoiceAmt@txtAddServiceTaxPer@txtServiceTaxAmt@txtFinalInvoiceAmt";
        vControlList = vControlList + '@cboCompany@cboContract@cboClient@cboPaymentMode@cboCompletedStatus@txtContractNo@txtInvoiceNo@txtInvoiceStartDate@txtInvoiceEndDate';

        var vAdditionalTrack = "TrnIvoiceID@@" + document.getElementById('hdnInvoiceID').value;

        callBack('InsertEditDetails', vControlList, vAdditionalTrack);
        $('#divInsertEdit').dialog('close');
    }
    $('#btnSave').attr('disabled', false);

    return;
}

function fn_FillClients() {
    var varSelVal = $("#cboAddCompany").val();
    if (varSelVal == "0") {
    }
    else {
        callBack('T_FillClients', 'cboAddCompany');
    }
    $('#cboAddClient').select2("val", "0");
    $('#cboAddContract').select2("val", "0");
    $('#cboAddTrnContract').select2("val", "0");
    $('#cboAddClient').find("option:gt(0)").remove();
    $('#cboAddContract').find("option:gt(0)").remove();
    $('#cboAddTrnContract').find("option:gt(0)").remove();
    fn_ClearAddControls();
}

function fn_FillContracts() {
    var varSelVal = $("#cboAddClient").val();
    if (varSelVal == "0") {
    }
    else {
        callBack('T_FillContracts', 'cboAddCompany@cboAddClient');
    }

    $('#cboAddContract').select2("val", "0");
    $('#cboAddTrnContract').select2("val", "0");
    $('#cboAddContract').find("option:gt(0)").remove();
    $('#cboAddTrnContract').find("option:gt(0)").remove();
    fn_ClearAddControls();
}

function fn_FillTrnContracts() {
    var varSelVal = $("#cboAddClient").val();
    if (varSelVal == "0") {
    }
    else {
        callBack('T_FillTrnContracts', 'cboAddCompany@cboAddClient@cboAddContract');
    }

    $('#cboAddTrnContract').select2("val", "0");
    $('#cboAddTrnContract').find("option:gt(0)").remove();
    fn_ClearAddControls();
}

function fn_FillTrnContractDetails() {
    var varSelVal = $("#cboAddTrnContract").val();
    if (varSelVal == "0") {
        fn_ClearAddControls();
    }
    else {
        callBack('T_FillTrnContractsDetails', 'cboAddTrnContract');
    }
}

function fn_FillTrnContractsDetails_Res(objResult) {

    document.getElementById('lblPaymentMode').innerText = objResult.lblPaymentMode;
    document.getElementById('lblPaymentLevel').innerText = objResult.lblPaymentLevel;
    document.getElementById('lblContractAmount').innerText = objResult.lblContractAmount;
    document.getElementById('txtAddContractPer').value = objResult.txtAddContractPer;

    document.getElementById('hdnCompletedContractPer').value = objResult.hdnCompletedContractPer;
    document.getElementById('hdnMaxContractInvoice').value = objResult.hdnMaxContractInvoice;
    document.getElementById('hdnCompletedInvAmount').value = objResult.hdnCompletedInvAmount;
    document.getElementById('hdnPaymentLevelID').value = objResult.hdnPaymentLevelID;

    fn_ValidateValues('ContractPer');
    if (objResult.IsLastPaymentMode == "True") {
        $("#txtAddContractPer").prop("readonly", true);
        $("#txtGrossInvoiceAmt").prop("readonly", true);
    }
    else {
        $("#txtAddContractPer").prop("readonly", false);
        $("#txtGrossInvoiceAmt").prop("readonly", false);
    }

}

function fn_ShowInvoiceDownload(objInvoiceID, objInvoiceNo, objClientName, objContractPath) {

    document.getElementById('spInvoiceNumber').innerHTML = objClientName + " has Invoice # <b>" + objInvoiceNo + "<b>";

    document.getElementById('cmdPrintRedirectToList').href = 'GenerateInvoicePDF.aspx?InvoiceID=' + objInvoiceID + "&HasHeader=0";
    document.getElementById('cmdPrintRedirectToListWithHeader').href = 'GenerateInvoicePDF.aspx?InvoiceID=' + objInvoiceID + "&HasHeader=1";
    document.getElementById('cmdDownloadContract').style.display = (objContractPath == "" ? "none" : "");
    document.getElementById('cmdDownloadContract').href = objContractPath;

    $('#divInvoiceNumber').dialog('open');
}

function fn_ValidateValues(objFlag) {
    if (document.getElementById('cboAddTrnContract') != '') {
        if (objFlag == "ContractPer") {
            var varContractPer = (document.getElementById("txtAddContractPer").value == "" ? 0 : parseFloat(document.getElementById("txtAddContractPer").value));
            if (varContractPer > 0) {
                var varTotContractInv = parseFloat(document.getElementById("lblContractAmount").innerText);
                var varInvAmount = Math.round(varTotContractInv * varContractPer / 100, 2);
                var varServiceTaxPer = parseFloat(document.getElementById("txtAddServiceTaxPer").value);
                var varServTaxAmt = Math.round(varServiceTaxPer > 0 ? varInvAmount * varServiceTaxPer / 100 : 0, 2);
                document.getElementById("txtGrossInvoiceAmt").value = varInvAmount;
                document.getElementById("txtServiceTaxAmt").value = varServTaxAmt;
                document.getElementById("txtFinalInvoiceAmt").value = (varInvAmount + varServTaxAmt);
            }
        }
        else if (objFlag == "GrossInvAmt") {
            var varGrossAmt = (document.getElementById("txtGrossInvoiceAmt").value == "" ? 0 : parseFloat(document.getElementById("txtGrossInvoiceAmt").value));
            if (varGrossAmt > 0) {
                var varTotContractInv = parseFloat(document.getElementById("lblContractAmount").innerText);
                var varContractPer = Math.round(varGrossAmt / varTotContractInv * 100, 2);
                var varServiceTaxPer = parseFloat(document.getElementById("txtAddServiceTaxPer").value);
                var varServTaxAmt = Math.round(varServiceTaxPer > 0 ? varGrossAmt * varServiceTaxPer / 100 : 0, 2);
                document.getElementById("txtAddContractPer").value = varContractPer;
                document.getElementById("txtServiceTaxAmt").value = varServTaxAmt;
                document.getElementById("txtFinalInvoiceAmt").value = (varGrossAmt + varServTaxAmt);
            }
        }
        else if (objFlag == "ContractPer") {
            var varContractPer = (document.getElementById("txtAddContractPer").value == "" ? 0 : parseFloat(document.getElementById("txtAddContractPer").value));
            if (varContractPer > 0) {
                var varGrossAmt = (document.getElementById("txtGrossInvoiceAmt").value == "" ? 0 : parseFloat(document.getElementById("txtGrossInvoiceAmt").value));
                var varServTaxAmt = Math.round(varContractPer > 0 ? varGrossAmt * varContractPer / 100 : 0, 2);
                document.getElementById("txtServiceTaxAmt").value = varServTaxAmt;
                document.getElementById("txtFinalInvoiceAmt").value = (varGrossAmt + varServTaxAmt);
            }
        }
    }
}

function fn_ValidateContractPer(p_obj, p_args) {
    p_args.IsValid = true;
    var varCntl = $("#" + p_obj.controltovalidate);
    var varValue = parseFloat(varCntl.val());
    if (varValue > 0) {
        var varCompletedPer = parseFloat(document.getElementById('hdnCompletedContractPer').value);
        if ((varCompletedPer + varValue) > 100) {
            p_args.IsValid = false;
            p_obj.childNodes['0'].innerHTML = "Contract % should be less than pending contract %";
        }
    }
}

function fn_ValidateInvoiceAmount(p_obj, p_args) {
    p_args.IsValid = true;
    var varCntl = $("#" + p_obj.controltovalidate);
    var varValue = parseFloat(varCntl.val());
    if (varValue > 0) {
        var varCompletedInv = parseFloat(document.getElementById('hdnCompletedInvAmount').value);
        var varTotContAmt = parseFloat(document.getElementById('lblContractAmount').innerText);
        if ((varCompletedInv + varValue) > varTotContAmt) {
            p_args.IsValid = false;
            p_obj.childNodes['0'].innerHTML = "Invoice Amount should be less than pending invoice amount";
        }
    }
}

function fn_ServiceTaxEnable() {
    if (document.getElementById('chkIsServiceTax').checked == false)
        document.getElementById('txtAddServiceTaxPer').value = '';
    else
        document.getElementById('txtAddServiceTaxPer').value = document.getElementById('hdnServiceTaxPer').value;
    fn_ValidateValues('ContractPer');
}

function fn_GetServiceTaxPer() {
    if (document.getElementById('txtAddInvoiceDate').text == '') {
        document.getElementById('hdnServiceTaxPer').value = '';
    }
    else {
        var vControlList = "txtAddInvoiceDate";
        callBack('GetServiceTaxPer', vControlList, '');
    }
} 

/**** Regular Expression Validator *****/
function fn_RegExpValidator(p_obj, p_args) {
    p_args.IsValid = true;
    var varCntl = $("#" + p_obj.controltovalidate);
    if (varCntl.attr("type") == "text") {
        if (varCntl.val() != "") {
            var vValidation = fn_ValidateExpression(varCntl.val(), $(p_obj).attr("expressionid"));
            if (vValidation != "") {
                args.IsValid = false;
                sender.childNodes['0'].innerHTML = vValidation;
            }
        }
    }
    return p_args.IsValid;

    //var ServiceLineName = document.getElementById('txtServiceLineName').value;
    //if (ServiceLineName != '') {
    //    var strRet = fn_ValidateExpression(ServiceLineName, '5');

    //    if (strRet != '') {
    //        var span = document.getElementById("spnServiceLineName");
    //        span.textContent = strRet;
    //        $('#spnServiceLineName').show();
    //        p_args.IsValid = false;
    //    }
    //}

}

function getResponse(returnVal) {
    try {
        hideLoader(returnVal);
        var objResult = $.parseJSON(returnVal);
        if (objResult.SessionTimeOut == "1") {
            window.location.href = 'login.aspx';
        }
        else {
            var varTrack = objResult.Track;

            if (objResult.Track == "GetSearchResult") {
                fn_GetSearchResult("1", objResult);
            }
            else if (objResult.Track == "InsertEditDetails") {
                if (objResult.IsSuccess == "1") {
                    if (parseInt($('#hdnContractID').val()) > 0)
                        show_globalMessage(objResult.strUpdateMessage, 'info');
                    else
                        show_globalMessage(objResult.strSaveMessage, 'info');
                    fn_GetSearchResult("1", objResult);
                }

                else if (objResult.IsSuccess == "0") {
                    show_globalMessage(objResult.strMessage, 'error');
                }
                else {
                    show_globalMessage(objResult.strMessage, 'error');
                }
            }
            else if (objResult.Track == "T_FillClients") {
                document.getElementById("cboAddClient").innerHTML = objResult.cboAddClient;
                $('#cboAddClient').select2();
            }
            else if (objResult.Track == "T_FillContracts") {
                document.getElementById("cboAddContract").innerHTML = objResult.cboAddContract;
                $('#cboAddContract').select2();
            }
            else if (objResult.Track == "T_FillTrnContracts") {
                document.getElementById("cboAddTrnContract").innerHTML = objResult.cboAddTrnContract;
                $('#cboAddTrnContract').select2();
            }
            else if (objResult.Track == "T_FillTrnContractsDetails") {
                fn_FillTrnContractsDetails_Res(objResult);
            }
            else if (objResult.Track == "EdiRecord") {
                fn_EdiRecord_Res(objResult);
            }
            else if (objResult.Track == "DownloadInvoice") {
                fn_ShowInvoiceDownload(objResult.InvoiceID, objResult.InvoiceNo, objResult.ClientName, objResult.ContractPath);
            }
            else if (objResult.Track == "GetServiceTaxPer") {
                document.getElementById("hdnServiceTaxPer").value = objResult.hdnServiceTaxPer;
                if (document.getElementById('chkIsServiceTax').checked == true) {
                    document.getElementById("txtAddServiceTaxPer").value = document.getElementById("hdnServiceTaxPer").value;
                    fn_ValidateValues('ContractPer');
                }
            }
            else if (objResult.Track == "DeleteRecord") {
                if (objResult.IsSuccess == "1") {
                    show_globalMessage(objResult.strMessage, 'info');
                    fn_GetSearchResult("1", objResult);
                }
                else if (objResult.IsSuccess == "0") {
                    show_globalMessage(objResult.strMessage, 'error');
                }
                else {
                    show_globalMessage(objResult.strMessage, 'error');
                }
            }
        }
    }
    catch (ex) {
        show_globalMessage('Error', 'error');
    }
}

